<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>条件渲染--元素变量</title>
</head>

<body>
  <div id="root">

  </div>
  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
  <script type="text/babel">
    function LoginButton(props) {
      return (
        <button onClick={props.onClick}>
          Login
    </button>
      );
    }

    function LogoutButton(props) {
      return (
        <button onClick={props.onClick}>
          Logout
    </button>
      );
    }
    class LoginControl extends React.Component {
      constructor(props) {
        super(props);

        this.state = { isLoggedIn: false };
      }

      handleLoginClick = () => {
        this.setState({ isLoggedIn: true });
      }

      handleLogoutClick = () => {
        this.setState({ isLoggedIn: false });
      }

      render() {
        const isLoggedIn = this.state.isLoggedIn;
        let button;
        if (isLoggedIn) {
          button = <LogoutButton onClick={this.handleLogoutClick} />;
        } else {
          button = <LoginButton onClick={this.handleLoginClick} />;
        }
        console.log(button)
        return (
          <div>
            {button}
          </div>
        );
      }
    }

    ReactDOM.render(
      <LoginControl />,
      document.getElementById('root')
    );
  </script>
</body>

</html>